package com.jhws.business.local.api;

import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.device.bo.DeviceConstant;
import com.jhws.business.local.service.LocalService;
import com.jhws.business.visitor.bo.TblVisitorDimBo;
import com.jhws.business.visitor.service.ITblVisitorDimService;
import com.jhws.common.base.bo.Global;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.util.AccountsUtils;
import com.jhws.util.DateUtil;
import com.jhws.util.StringUtils;
import com.jhws.util.commUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Controller
@RequestMapping("/localCall")
public class LocalCallApi {

    @Resource
    private ITblCommDimService ICommService;
    @Resource
    private ITblBuildingDimService IBuildingDimService;
    @Resource
    private ITblVisitorDimService visitorDimService;
    @Resource
    private LocalService localService;

    /**
     * 保存呼叫信息
     *
     * @return
     */
    @RequestMapping(value = "anon/saveCallInfo")
    @ResponseBody
    public JsonRespData saveCallInfo(@RequestParam("image") MultipartFile image,
                                     String deviceNum,
                                     String calledNum,
                                     String date,
                                     String type,
                                     String fileName) throws Exception {
        if (StringUtils.isEmpty(type)) {
            return new JsonRespData("", -1, "类型不能为空！");
        }
        if (date == null) {
            return new JsonRespData("", -1, "日期不能为空！");
        }
        if (StringUtils.isEmpty(deviceNum)) {
            return new JsonRespData("", -1, "设备帐号不能为空！");
        }

        String imageUrl = localService.saveCallImage(image, fileName);
        TblVisitorDimBo visitorDimBo = new TblVisitorDimBo();
        String commCode = AccountsUtils.getCommCode(deviceNum);
        int calledType = AccountsUtils.devicesType(calledNum);
        TblCommDimBo commDimBo;
        if (Global.VISITOR_CALL.equals(type)) {
            if (StringUtils.isEmpty(calledNum)) {
                return new JsonRespData("", -1, "被叫帐号不能为空！");
            }
            if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.OUTDOOR_TYPE) {
                commDimBo = ICommService.findTblCommDimByCommCode(commCode);
                String areaName = AccountsUtils.getBuilding(deviceNum);
                String buildingName = AccountsUtils.getUnit(deviceNum);
                String buildingAliasName = IBuildingDimService.getBuildingAliasName(commCode, areaName, buildingName);
                String unitAliasName = IBuildingDimService.getUnitAliasName(commCode, areaName, buildingName);
                String deviceName;
                deviceName = "[" + commDimBo.getCommName() + "] " + buildingAliasName + " " + unitAliasName + " 门口机-" + deviceNum.substring(11, 12);
                visitorDimBo.setBuildingAlias(buildingAliasName);
                visitorDimBo.setDeviceType(DeviceConstant.OUTDOOR_TYPE);
                visitorDimBo.setDeviceNum(deviceNum);
                visitorDimBo.setAccessDevice(deviceName);
                visitorDimBo.setType(Global.VISITOR_CALL);
                visitorDimBo.setVisitorDate(DateUtil.stringDate(date));
                visitorDimBo.setCalledNum(calledNum);
                visitorDimBo.setCommName(commDimBo.getCommName());
                visitorDimBo.setCommCode(commCode);
                visitorDimBo.setBuildingAlias(buildingAliasName);
                visitorDimBo.setCalledType(calledType);
                if (calledType == DeviceConstant.INDOOR_TYPE || calledType == DeviceConstant.MANAGE_TYPE) {
                    visitorDimBo.setBuildingCode(AccountsUtils.getBuilding(calledNum));
                    visitorDimBo.setUnitCode(AccountsUtils.getUnit(calledNum));
                    visitorDimBo.setFloorCode(AccountsUtils.getFloor(calledNum));
                    visitorDimBo.setRoomCode(AccountsUtils.getRoom(calledNum));
                } else {
                    visitorDimBo.setBuildingCode(AccountsUtils.getBuilding(deviceNum));
                    visitorDimBo.setUnitCode(AccountsUtils.getUnit(deviceNum));
                    visitorDimBo.setFloorCode(AccountsUtils.getFloor(deviceNum));
                    visitorDimBo.setRoomCode(AccountsUtils.getRoom(deviceNum));
                }
                visitorDimBo.setImageUrl(imageUrl);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.VILLA_TYPE) {
                visitorDimBo.setDeviceType(DeviceConstant.VILLA_TYPE);
                visitorDimBo.setDeviceNum(deviceNum);
                visitorDimBo.setAccessDevice("别墅机-" + deviceNum);
                visitorDimBo.setType(Global.VISITOR_CALL);
                visitorDimBo.setVisitorDate(DateUtil.stringDate(date));
                visitorDimBo.setCalledNum(calledNum);
                visitorDimBo.setCommName("别墅");
                visitorDimBo.setCalledType(calledType);
                visitorDimBo.setImageUrl(imageUrl);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.WALL_TYPE) {
                commDimBo = ICommService.findTblCommDimByCommCode(commCode);
                visitorDimBo.setDeviceType(DeviceConstant.WALL_TYPE);
                visitorDimBo.setDeviceNum(deviceNum);
                visitorDimBo.setAccessDevice(commUtil.getGateDevAliasName(deviceNum, commDimBo.getGateDevAliasName()));
                visitorDimBo.setType(Global.VISITOR_CALL);
                visitorDimBo.setVisitorDate(DateUtil.stringDate(date));
                visitorDimBo.setCalledNum(calledNum);
                visitorDimBo.setCommName(commDimBo.getCommName());
                visitorDimBo.setCommCode(commCode);
                visitorDimBo.setCalledType(calledType);
                if (calledType == DeviceConstant.INDOOR_TYPE || calledType == DeviceConstant.MANAGE_TYPE) {
                    String building = AccountsUtils.getBuilding(calledNum);
                    String unit = AccountsUtils.getUnit(calledNum);
                    String buildingAliasName = IBuildingDimService.getBuildingAliasName(commCode, building, unit);
                    visitorDimBo.setBuildingAlias(buildingAliasName);
                    visitorDimBo.setBuildingCode(building);
                    visitorDimBo.setUnitCode(unit);
                    visitorDimBo.setFloorCode(AccountsUtils.getFloor(calledNum));
                    visitorDimBo.setRoomCode(AccountsUtils.getRoom(calledNum));
                } else {
                    visitorDimBo.setBuildingCode(AccountsUtils.getBuilding(deviceNum));
                    visitorDimBo.setUnitCode(AccountsUtils.getUnit(deviceNum));
                    visitorDimBo.setFloorCode(AccountsUtils.getFloor(deviceNum));
                    visitorDimBo.setRoomCode(AccountsUtils.getRoom(deviceNum));
                }
                visitorDimBo.setImageUrl(imageUrl);
            } else {
                return new JsonRespData("", -1, "设备帐号错误！");
            }
            visitorDimService.save(visitorDimBo);
        } else {
            return new JsonRespData("", -1, "类型不存在！");
        }
        return new JsonRespData("", 1, "保存成功");
    }
}
